//
//  BaseViewModel.m
//  ShandongPoker
//
//  Created by Developer on 2025-01-08.
//  Copyright © 2025 ShandongPoker. All rights reserved.
//

#import "BaseViewModel.h"

@interface BaseViewModel ()

@property (nonatomic, assign, readwrite) BOOL isLoading;
@property (nonatomic, strong, readwrite, nullable) NSError *error;

@end

@implementation BaseViewModel

#pragma mark - Initialization

- (instancetype)init {
    if (self = [super init]) {
        _isLoading = NO;
        _error = nil;
    }
    return self;
}

#pragma mark - Public Methods

- (void)setLoading:(BOOL)loading {
    if (_isLoading != loading) {
        [self willChangeValueForKey:@"isLoading"];
        _isLoading = loading;
        [self didChangeValueForKey:@"isLoading"];
        
        if (self.loadingStateDidChange) {
            self.loadingStateDidChange(loading);
        }
    }
}

- (void)setError:(NSError *)error {
    if (_error != error) {
        [self willChangeValueForKey:@"error"];
        _error = error;
        [self didChangeValueForKey:@"error"];
        
        if (error && self.errorDidOccur) {
            self.errorDidOccur(error);
        }
    }
}

- (void)clearError {
    [self setError:nil];
}

@end